package com.gdut.simple.view.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.gdut.simple.R;
import com.gdut.simple.data.entity.PhotoFolder;
import com.gdut.simple.view.adapter.PhotoFolderAdapter;
import com.gdut.simple.view.adapter.base.AbsRecyclerAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PhotoSpinnerWindow extends PopupWindow {

    public PhotoSpinnerWindow(Context context, final List<PhotoFolder> list, final OnItemSelectedListener listener) {
        LayoutInflater inflater = LayoutInflater.from(context);
        RecyclerView view = new RecyclerView(context);
        view.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view.setLayoutManager(new LinearLayoutManager(context));
        PhotoFolderAdapter adapter = new PhotoFolderAdapter(context, list);
        view.setAdapter(adapter);
        adapter.setOnItemClickListener(new AbsRecyclerAdapter.DefaultItemClickListener() {

            @Override
            public void onClick(View view, int position) {
                String dir = list.get(position).getDir();
                String name = list.get(position).getName();
                File file = new File(dir);
                if (file.list() != null) {
                    List<String> images = new ArrayList<>();
                    for (String path : file.list()) {
                        images.add(list.get(position).getDir() + File.separator + path);
                    }
                    if (listener != null) {
                        listener.onSelected(view, dir, name, images);
                    }
                }
            }
        });
        this.setContentView(view);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        ColorDrawable bd = new ColorDrawable(0xb0000000);
        this.setBackgroundDrawable(bd);
        this.setAnimationStyle(R.style.bottom_popup_anim);
    }

    public interface OnItemSelectedListener {
        void onSelected(View view, String dir, String name, List<String> images);
    }

}
